<template>
  <page-header-wrapper :title="false">
    <a-card title="调价手机清单" style="margin-top: 24px">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(queryParam)" :loading="recordLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
                <a-button type="danger" style="margin: 0px 5px" @click="setFq()">放弃</a-button>
                <a-button
                  type="success"
                  style="margin: 0px 5px; background: #87b87f !important; color: #fff"
                  @click="setBack()"
                >返回</a-button
                >
                <a-button type="primary" style="margin: 0px 5px" @click="setSubmit()">提交</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428BCA" @click="handleEdit(record)">编辑</a-tag>
          </template>
          <template>
            <a-tag color="#D15B47" @click="handleDelete(record)">删除</a-tag>
          </template>
        </span>
      </a-table>
      <price-form
        ref="priceModal"
        :visible="priceVisible"
        :loading="priceLoading"
        :model="priceMdll"
        @cancel="handlePriceCancel"
        @ok="handlePriceOk"
      />
    </a-card>
  </page-header-wrapper>
</template>
  
<script>
import { getChgDetailListUsingPOST, updateChgDetailUsingPOST } from '@/api/api/kucunguanli.ts'
import PriceForm from './PriceForm'
export default {
  name: 'ChangePriceRecordChange',
  components: {
    PriceForm,
  },
  data() {
    return {
      mdl: null,
      visible: false,
      confirmLoading: false,
      priceVisible: false,
      priceLoading: false,
      priceMdll: null,
      turnMdl: null,
      turnVisible: false,
      turnConfirmLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      pagination: {},
      advanced: false,
      detail: {},
      recordLoading: false,
      columns: [
        {
          title: '调价日期',
          dataIndex: 'operDate',
        },
        {
          title: '商品',
          dataIndex: 'goodsName',
        },
        {
          title: '串码',
          dataIndex: 'phoneCode1',
        },
        {
          title: '变化金额(元)',
          dataIndex: 'chgAmount',
        },
        {
          title: '调价店面',
          dataIndex: 'storeName',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loadData: [],
    }
  },
  mounted() {
    this.handleSearch(this.queryParam)
  },
  methods: {
    handlePriceCancel() {
      this.priceVisible = false
      const form = this.$refs.priceModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handlePriceOk() {
      const form = this.$refs.priceModal.form
      this.priceLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          // 修改 e.g.
         this.loadData = this.loadData.map(item => {
            return {
              ...item,
              chgAmount: values.chgAmount
            }
          })
          this.priceLoading = false
          this.priceVisible = false
        } else {
          this.priceLoading = false
          this.priceVisible = false
        }
      })
    },
    setSubmit() {
      const that = this
      that.$confirm({
        title: '提交',
        content: '你确定已经仔细核对销售数据了吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          const params = that.loadData.map((item) => {
            return {
              chgAmount: item.chgAmount,
              id: item.id,
              inventoryId: item.inventoryId,
              orderId: that.$route.params.id,
            }
          })
          updateChgDetailUsingPOST([...params]).then(() => {
            that.$message.success('操作成功')
            that.$router.back()
          })
          .catch((error) => {
            that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
          })
        },
      })
    },
    setFq() {
      const that = this
      that.$confirm({
        title: '放弃',
        content: '你确定放弃修改，重新编辑吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          that.handleSearch(that.queryParam)
        },
      })
    },
    setBack() {
      const that = this
      that.$confirm({
        title: '返回',
        content: '你确定放弃修改，返回订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          that.$router.back()
        },
      })
    },
    handleEdit(record) {
      this.priceVisible = true
      this.priceMdll = record
    },
    handleDelete(record) {
      const that = this
      that.$confirm({
        title: '删除',
        content: '你确定要删除这个商品吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          that.loadData.splice(
            that.loadData.findIndex((item) => item.id === record.id),
            1
          )
        },
      })
    },
    handleSearch(params) {
      this.recordLoading = true
      getChgDetailListUsingPOST({ ...params, orderId: this.$route.params.id })
        .then((res) => {
          this.loadData = res.data.list
          this.recordLoading = false
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleRecordTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.recordPagination = pager
      this.handleRecordSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: '',
        status: '',
      })
    },
  },
}
</script>
  